<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>不同源的子页面</title>
  </head>
  <body>
    <h2>不同源的子页面</h2>
    <button class="btn">向父页面发送数据</button>
    <script>
      // 接受父页面发送的数据
      window.onmessage = function (e) {
        console.log(e.data); // 父页面通信的数据
        document.querySelector("h2").innerText = e.data.title;
      };

      document.querySelector(".btn").onclick = function () {
        // 子页面向父页面发送数据
        window.parent.postMessage("hello father", "http://127.0.0.1:5500");
      };
    </script>
  </body>
</html>
